import os
import sys

from PySide6.QtWidgets import QWidget, QGridLayout, QLabel, QPushButton, QLineEdit, QMessageBox, QApplication, QComboBox
from easy_tools.utils import replace_dirs


class StringReplaceWidget(QWidget):

    def __init__(self):
        super().__init__()
        self._init()

    def _init(self):
        grid = QGridLayout(self)
        self.setLayout(grid)
        self.edit_path = self._get_text_edit()
        self.generate_path = self._get_text_edit()
        self.old = self._get_text_edit()
        self.new = self._get_text_edit()
        self.encoding = self._get_text_edit('utf-8')
        self.confirm = QPushButton("替换", self)
        self.ignore_case = QComboBox(self)
        self.ignore_case.addItem('否', False)
        self.ignore_case.addItem('是', True)
        self.include_suffix = self._get_text_edit('*')
        self.exclude_suffix = self._get_text_edit()

        x = 0
        grid.addWidget(QLabel("需要替换的目录/文件地址: ", self), x, 0, 1, 2)
        grid.addWidget(self.edit_path, x, 2, 1, 4)
        x += 1
        grid.addWidget(QLabel("生成的目录: ", self), x, 0, 1, 2)
        grid.addWidget(self.generate_path, x, 2, 1, 4)
        x += 1
        grid.addWidget(QLabel("被替换文本: ", self), x, 0)
        grid.addWidget(self.old, x, 1)
        grid.addWidget(QLabel("替换文本: ", self), x, 2)
        grid.addWidget(self.new, x, 3)
        grid.addWidget(QLabel('忽略大小写: ', self), x, 4)
        grid.addWidget(self.ignore_case, x, 5)
        x += 1
        grid.addWidget(QLabel("文件编码: ", self), x, 0)
        grid.addWidget(self.encoding, x, 1)
        grid.addWidget(QLabel('包含后缀: ', self), x, 2)
        grid.addWidget(self.include_suffix, x, 3)
        grid.addWidget(QLabel('排除后缀: ', self), x, 4)
        grid.addWidget(self.exclude_suffix, x, 5)
        x += 1
        grid.addWidget(self.confirm, x, 0, 1, 6)
        for i in range(x + 1):
            grid.setRowStretch(i, 1)

        self.confirm.clicked.connect(self._replace_str)

    def _replace_str(self):
        if self._check_input():
            try:
                input_path = self.edit_path.text()
                output_path = self.generate_path.text()
                if not output_path.endswith(os.sep):
                    output_path += os.sep
                output_path += input_path.split(os.sep)[-1]
                include_suffix = [one.replace('.', '').strip() for one in self.include_suffix.text().split(',')]
                exclude_suffix = [one.replace('.', '').strip() for one in self.exclude_suffix.text().split(',')] if self.exclude_suffix.text().strip() else tuple()
                replace_dirs(input_path,
                             output_path,
                             self.old.text(),
                             self.new.text(),
                             encoding=self.encoding.text(),
                             ignore_case=self.ignore_case.currentData(),
                             include_suffix=include_suffix,
                             exclude_suffix=exclude_suffix)
                QMessageBox.question(self, "通知", "替换完成", QMessageBox.StandardButton.Ok)
            except Exception as e:
                QMessageBox.question(self, "警告", str(e), QMessageBox.StandardButton.Ok)

    def _check_input(self):
        if not self.edit_path.text():
            QMessageBox.question(self, "错误", "替换地址不能为空", QMessageBox.StandardButton.Ok)
            return False
        if not self.generate_path.text():
            self.generate_path.setText(self.edit_path.text())
        if not self.old.text():
            QMessageBox.question(self, "错误", "被替换文本不能为空", QMessageBox.StandardButton.Ok)
            return False
        if not self.encoding:
            QMessageBox.question(self, "错误", "文件编码不能为空", QMessageBox.StandardButton.Ok)
            return False
        return True

    def _get_text_edit(self, text=""):
        e = QLineEdit(text, self)
        e.resize(50, 20)
        return e


if __name__ == '__main__':
    app = QApplication()
    win = StringReplaceWidget()
    win.show()
    sys.exit(app.exec())
